﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace EzyThirdLogic
{
    public class FunctionalDependencySet : IComparable
    {
        #region Private Fields

        private ISet<FunctionalDependency> _fdSet;

        #endregion

        #region Constructors

        public FunctionalDependencySet()
        {
            _fdSet = new SortedSet<FunctionalDependency>();
        }

        public FunctionalDependencySet(string[][] fds, int max)
        {
            _fdSet = new SortedSet<FunctionalDependency>();
            for (int i = 0; i < max; i++)
            {
                if (fds[i][0].Length != 0 && fds[i][1].Length != 0)
                {
                    _fdSet.Add(new FunctionalDependency(fds[i][0], fds[i][1]));
                }
            }
        }

        public FunctionalDependencySet(FunctionalDependencySet fdSet)
        {
            _fdSet = new SortedSet<FunctionalDependency>(fdSet.FDSet);
        }

        public FunctionalDependencySet(IEnumerable<FunctionalDependency> fdSet)
        {
            _fdSet = new SortedSet<FunctionalDependency>(fdSet);
        }

        #endregion 

        #region Properties

        public ISet<FunctionalDependency> FDSet
        {
            get { return _fdSet; }
            set { _fdSet = value; }
        }

        #endregion

        #region Public Methods

        public void AddFunctionalDependency(FunctionalDependency fd)
        {
            _fdSet.Add(fd);
        }

        public FunctionalDependencySet MergeFunctionalDependencySets(FunctionalDependencySet fdSet1, FunctionalDependencySet fdSet2)
        {
            FunctionalDependencySet mergedFDSet = new FunctionalDependencySet(fdSet1);
            
            foreach (FunctionalDependency fd in fdSet2.FDSet)
            {
                mergedFDSet.AddFunctionalDependency(fd);
            }

            return mergedFDSet;
        }

        public void RemoveFunctionalDependency(FunctionalDependency fd)
        {
            _fdSet.Remove(fd);
        }

        public override string ToString()
        {
            string result = "";

            for (int i = 0; i < FDSet.Count; i++)
            {
                result += FDSet.ElementAt<FunctionalDependency>(i).ToString();

                if (i != FDSet.Count - 1)
                {
                    result += "\n";
                }
            }

            return result;
        }

        public string ToString2()
        {
            string result = "";

            for (int i = 0; i < FDSet.Count; i++)
            {
                result += FDSet.ElementAt<FunctionalDependency>(i).ToString();

                if (i != FDSet.Count - 1)
                {
                    result += ", ";
                }
            }

            return result;
        }

        public int CompareTo(object fdSet)
        {
            return this.ToString().CompareTo(fdSet.ToString());
        }

        #endregion
    }
}
